package com.lce.controller;

import com.github.pagehelper.PageInfo;
import com.lce.beans.Dept;
import com.lce.beans.User;
import com.lce.service.CeoService;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


@Controller
public class CeoController {
    private CeoService ceoService;

    public CeoService getCeoService() {
        return ceoService;
    }

    @Resource
    public void setCeoService(CeoService ceoService) {
        this.ceoService = ceoService;
    }

    //查看所有员工
    @RequestMapping("/ceolookalluser")
    public String getAllUser(HttpServletRequest request, @RequestParam(defaultValue = "1") Integer pageNo, @RequestParam(defaultValue = "5") Integer pageSize) {

        HashMap<String, Object> map = ceoService.getalluser(pageNo, pageSize);
        request.setAttribute("users", map.get("data"));
        request.setAttribute("nowPage", map.get("nowPage"));
        request.setAttribute("total", map.get("total"));
        //查询数据总条数
        long couts = ceoService.counts();
        request.setAttribute("couts", couts);
        return "ceolookusers";
    }

    //ajax查看所有部门
    @RequestMapping("/ceoGetAllDepts")
    @ResponseBody
    public Object getAllDepts(HttpServletRequest request) {
        System.out.println(12);
        List<Dept> depts = ceoService.ceoGetAllDepts();
        return depts;
    }

    //添加员工
    @RequestMapping(value = "/ceoadduser", method = {RequestMethod.POST})
    public String addUser(@RequestParam("file") MultipartFile file, User user, HttpServletRequest request) throws IOException {
        //上传文件路径
        String realPath = request.getSession().getServletContext().getRealPath("resource/uploadimage");
        //得到文件上传的名称
        String originalFilename = file.getOriginalFilename();
        //创建上传目录
        File getFile = new File(realPath);
        if (!getFile.exists()) {
            getFile.mkdir();
        }
        //把file文件写道指定路径下
        try {
            file.transferTo(new File(getFile + "/" + originalFilename));
            String image = "/resource/uploadimage/" + originalFilename;
            user.setImage(image);
            //处理日期时间类
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String date = request.getParameter("date");
            Date birthday = simpleDateFormat.parse(date);
            user.setBirthday(birthday);
            //生成唯一的员工编号
            String ucard = new Date().getTime() + "";
            user.setUcard(ucard);
            //执行添加方法
            ceoService.addUser(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/ceolookalluser";
    }

    //删除指定员工
    @RequestMapping("/ceodeleteuser")
    public String ceodeleteuser(int id, HttpServletRequest request) {
        ceoService.deleteUser(id);
        request.getSession().setAttribute("msg", "删除成功！");
        return "redirect:/ceolookalluser";
    }

    //修改前获取指定员工信息,用于回显
    @RequestMapping("/ceogetuserbyid")
    public String ceogetuserbyid(int id, HttpServletRequest request) {
        User user = ceoService.getUserById(id);
        System.out.println("修改前获取到的user" + user);
        request.setAttribute("userdetails", user);

        return "ceoupuser";
    }

    //修改指定员工
    @RequestMapping("/ceoupdateuser")
    public String ceoupdateuser(@RequestParam("file") MultipartFile file, User user, HttpServletRequest request) {
        //上传文件路径
        String realPath = request.getSession().getServletContext().getRealPath("resource/uploadimage");
        //得到文件上传的名称
        String originalFilename = file.getOriginalFilename();
        //创建上传目录
        File getFile = new File(realPath);
        if (!getFile.exists()) {
            getFile.mkdir();
        }
        //把file文件写道指定路径下
        try {
            file.transferTo(new File(getFile + "/" + originalFilename));
            String image = "/resource/uploadimage/" + originalFilename;
            user.setImage(image);
            //处理日期时间类
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String date = request.getParameter("date");
            Date birthday = simpleDateFormat.parse(date);
            user.setBirthday(birthday);
            //执行修改方法
            ceoService.updateUser(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        request.getSession().setAttribute("msg", "修改成功！");
        return "redirect:/ceolookalluser";
    }


}
